package model

import (
	"gorm.io/gorm"
	"wechat/handle/response"
)

type Moment struct {
	gorm.Model
	UserId  int    `gorm:"type:int" json:"userId"`
	Content string `gorm:"type:varchar(30)" json:"content"`
	Count   int    `gorm:"type:int" json:"count"`
}

// 发布朋友圈
func (m *Moment) AddMoment(db *gorm.DB) error {
	return db.Debug().Create(&m).Error
}

// 朋友圈列表
func (m *Moment) ListMoment(db *gorm.DB, userId int) (list []*response.ListMoment, err error) {
	err = db.Debug().Model(&m).
		Select("users. *,moments. *", "users.id as UserId", "users.phone as Phone", "users.username as Username",
			"users.image as Image", "users.created_at as Created", "moments.content as Content", "moments.count as Count").
		Joins("left join users on users.id = moments.user_id").
		Where("moments.user_id=?", userId).Order("moments.created_at desc").Find(&list).Error
	return list, err
}
